#include "inputhandler.h"
#include "../transformation/camera.h"
#include "../math/vector.h"
#include "windows.h"
#include <iostream>
using namespace std;

// ================================================================================================
// Handle camera input
// ================================================================================================
bool InputHandler::HandleCameraInput(Camera& camera) {
    if(GetAsyncKeyState(VK_LEFT) != 0) {
        camera.RotateViewY(-0.1f);
    }

    if(GetAsyncKeyState(VK_RIGHT) != 0) {
        camera.RotateViewY(0.1f);
    }

    if(GetAsyncKeyState(VK_UP) != 0) {
        Vector4 up(0.0f, 0.1f, 0.0f);
        camera.Translate(up);
    }

    if(GetAsyncKeyState(VK_DOWN) != 0) {
        Vector4 up(0.0f, -0.1f, 0.0f);
        camera.Translate(up);
    }

    if(GetAsyncKeyState(eAKey) != 0) {
        Vector4 left(0.1f, 0.0f, 0.0f);
        camera.Translate(left);
    }

    if(GetAsyncKeyState(eDKey) != 0) {
        Vector4 left(-0.1f, 0.0f, 0.0f);
        camera.Translate(left);
    }

    if(GetAsyncKeyState(eSKey) != 0) {
        Vector4 at(0.0f, 0.0f, -0.1f);
        camera.Translate(at);
    }

    if(GetAsyncKeyState(eWKey) != 0) {
        Vector4 at(0.0f, 0.0f, 0.1f);
        camera.Translate(at);
    }

    if(GetAsyncKeyState(VK_ESCAPE) !=0)
        return true;

    return false;
}

// ================================================================================================
// Handle Camera Mouse Input
// ================================================================================================
void InputHandler::HandleCameraMouseInput(Camera& camera, POINT& startpt, POINT& center) {
    float mousescalemovement = 0.002f;
    POINT endpt;
    GetCursorPos(&endpt);

    int deltaptx = endpt.x - startpt.x;
    int deltapty = endpt.y - startpt.y;

    if(deltaptx != 0)
        camera.RotateViewY(float(deltaptx) * mousescalemovement);
    if(deltapty != 0)
        camera.RotateViewX(float(-deltapty) * mousescalemovement);

    // -- refresh the cursors
    SetCursorPos(center.x, center.y);
}